/*

	This file prepares regressions sample at product-destination-year level

*/

cd ${work}5_reg_analysis

use jurnr vnr land year vrd_exp_exsmall using input\export_ipdt if year>2002 & vrd_exp_exsmall!=. , clear

*PRODUCT GROUPS
merge m:1 vnr using input\productcodes_p, assert(2 3) keep(3) keepusing(cn2 cn4) nogen

* COLLAPSE 
replace land="BE" if land=="LU" //to comply with BACI
collapse (sum) vrd_exp_exsmall , by(cn2 cn4 land year)

*PROMOTION
merge m:1 cn4 land year using input\promotion_pdt, keep(1 3) keepusing(treated_sum_2 tdcinitiative_sum_2) nogen
foreach v of varlist treated_sum_2 tdcinitiative_sum_2 {
	replace `v'=0 if `v'==.
}

*BACI
preserve
	use input\baci_pdt, replace
	keep if year>2002
	gen cn4_num=floor(cn6/100)
	collapse (sum) value , by(cn4_num land year)
	save temp\temp, replace
restore
destring cn4, gen(cn4_num)
merge 1:1 cn4_num land year using temp\temp, keep(3) keepusing(value) nogen
erase temp\temp.dta

*SPECIFICATOINS
winsor2 vrd_exp_exsmall value , cuts(0 99) replace
gen log_vrd_exp_exsmall=log(vrd_exp_exsmall)
gen log_value = log(value)

lab var log_vrd_exp_exsmall	"ln(EXV)"
lab var log_value "ln(BACI)"

*FEs
egen P2DY=group(cn2 land year)
egen P4D = group(cn4 land)
egen P4Y = group(cn4 year)

*SAVE
compress
save temp\regsample_pdt, replace